//
//  ImagesAnimateScrollView.swift
//  ContactPoster
//
//  Created by TSYH on 2024/3/4.
//

import UIKit

class ImageAnimationRowView: UIView {
    enum `Direction`: Int {
        case leftToRight    = 0
        case rightToLeft    = 1
        case topToBottom    = 2
        case bottomToTop    = 3
    }
    
    lazy var imageView1 = UIImageView()
    lazy var imageView2 = UIImageView()
    
    lazy var direction: `Direction` = .leftToRight
    lazy var margin: CGFloat = 0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        setupObser()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        clipsToBounds = true
        setupObser()
    }
    
    func setupObser() {
        NotificationCenter.default.addObserver(self, selector: #selector(onAppActiveNotify(_:)), name: UIApplication.didBecomeActiveNotification, object: nil)
    }
    
    @objc func onAppActiveNotify(_ notify: Notification) {
        DispatchQueue.main.async {
            self.startAnimation()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupSubviews()
    }
    
    func set(imageName: String, size: CGSize) {
        if let image = UIImage(named: imageName) {
            imageView1.image = image
            imageView2.image = image
        }
        imageView1.frame.size = size
        imageView2.frame.size = size
    }
    
    func setupSubviews() {
        if imageView1.superview == nil {
            addSubview(imageView1)
            addSubview(imageView2)
        
            switch direction {
            case .leftToRight:
                imageView1.x = width-imageView1.width
                imageView1.y = 0
                imageView2.y = 0
                imageView2.x = imageView1.x-imageView2.width-margin
            case .rightToLeft:
                imageView1.x = 0
                imageView1.y = 0
                imageView2.y = 0
                imageView2.x = imageView1.width+margin
            case .topToBottom:
                imageView1.x = 0
                imageView1.y = height-imageView1.height
                imageView2.x = 0
                imageView2.y = imageView1.y-imageView2.height-margin
            case .bottomToTop:
                imageView1.x = 0
                imageView1.y = 0
                imageView2.x = 0
                imageView2.y = imageView1.height+margin
            }
        }
    }
    
    func startAnimation() {
        
        for iv in [imageView1, imageView2] {
            iv.layer.removeAllAnimations()
        }
        
        var moveAnim = CABasicAnimation(keyPath: "position.x")
        switch direction {
        case .leftToRight:
            moveAnim.byValue = imageView1.width+margin
        case .rightToLeft:
            moveAnim.byValue = -imageView1.width-margin
        case .topToBottom:
            moveAnim = CABasicAnimation(keyPath: "position.y")
            moveAnim.byValue = imageView1.height+margin
        case .bottomToTop:
            moveAnim = CABasicAnimation(keyPath: "position.y")
            moveAnim.byValue = -imageView1.height-margin
        }
        moveAnim.duration = 40
        moveAnim.repeatCount = .greatestFiniteMagnitude
        imageView1.layer.add(moveAnim, forKey: "move")
        imageView2.layer.add(moveAnim, forKey: "move")
    }
}
